package ajjj.kiviq.beans;

import ajjj.kiviq.entities.Category;
import ajjj.kiviq.remoteInterfaces.IRemoteCategory;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.*;

/**
 *  Updated 2011-10-11 by jonatan
 *  Added methods to check whether there are any categories
 * 
 * @author orjohan
 * 
 */
@ManagedBean(name = "CategoryBean")
@ViewScoped
public class CategoryBean implements Serializable {

    private DateBean datebean;
    @EJB
    private IRemoteCategory cf;
    private List<Category> categories;
    private List<Category> publicCategories;
    private String name;
    @ManagedProperty(value = "#{UserBean}")
    private UserBean ub;

    /**
     * Default constructor
     */
    public CategoryBean() {
    }

    /**
     * Initializes this CategoryBean, this
     * method is called by the EJB container
     */
    @PostConstruct
    private void init() {
	name = ub.currentUser();
	categories = cf.getCategories(name);
	publicCategories = cf.getPublicCategories();
    }

    /**
     * Check whether there are any public categories
     * in the database
     * @return True if there are any public categories,
     * false otherwise
     */
    public boolean areAnyPublicCategories() {
	return !getPublicCategories().isEmpty();
    }

    /**
     * Check whether there are any private categories,
     * private categories are considered all categories
     * created by the current user
     * @return True if there are any private categories
     * in the database, false otherwise
     */
    public boolean areAnyPrivateCategories() {
	return !getCategories().isEmpty();
    }

    /**
     * Get the private categories, private categories
     * are considered all categories created by the
     * current user
     * @return The private categories
     */
    public List<Category> getCategories() {
	return categories;
    }

    /**
     * Get the public categories
     * @return All public categories
     */
    public List<Category> getPublicCategories() {
	return publicCategories;
    }

    /**
     * Get the name of the current user
     * @return The name of the current user
     */
    public String getName() {
	return name;
    }

    /**
     * Get the current date and time
     */
    public String getDate() {
	return datebean.getCurrentDateTime();
    }

    /**
     * Get the UserBean associated to this CategoryBean
     * @return The UserBean
     */
    public UserBean getUb() {
        return ub;
    }

    /**
     * Set the UserBean associated to this CategoryBean
     * @param ub The UserBean
     */
    public void setUb(UserBean ub) {
        this.ub = ub;
    }
    
}